package com.makeunion.gdlbsdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusPath;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.DriveStep;
import com.amap.api.services.route.RidePath;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.TMC;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkRouteResult;
import com.makeunion.gdlbsdemo.R;
import com.makeunion.gdlbsdemo.overlay.BusRouteOverlay;
import com.makeunion.gdlbsdemo.overlay.DrivingRouteOverlay;
import com.makeunion.gdlbsdemo.overlay.RideRouteOverlay;
import com.makeunion.gdlbsdemo.overlay.WalkRouteOverlay;

import java.util.List;

/**
 * 类说明
 *
 * @author renjialiang
 * @version [版本]
 * @see [相关类]
 * @since [模块]
 */
public class RouteSearchActivity extends AppCompatActivity implements RouteSearch.OnRouteSearchListener {

    private MapView mapView;

    private AMap aMap;

    private LatLonPoint startPoint = new LatLonPoint(39.742295, 116.235891);
    private LatLonPoint endPoint = new LatLonPoint(39.995576, 116.481288);

    private RouteSearch routeSearch;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_route_search);

        mapView = (MapView) findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState); //此方法必须重写
        init();
        addMarkers();
    }

    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        routeSearch = new RouteSearch(this);
        routeSearch.setRouteSearchListener(this);
    }

    /**
     * 添加标记.
     */
    private void addMarkers() {
        LatLng start = new LatLng(startPoint.getLatitude(), startPoint.getLongitude());
        aMap.addMarker(new MarkerOptions()
                .position(start)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.start)));
        LatLng end = new LatLng(endPoint.getLatitude(), endPoint.getLongitude());
        aMap.addMarker(new MarkerOptions()
                .position(end)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.end)));
    }

    /**
     * 驾车模式.
     * @param view view
     */
    public void driveMode(View view) {
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
                startPoint, endPoint);
        RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(
                fromAndTo, //路径规划的起点和终点
                RouteSearch.DrivingDefault, //驾车模式
                null, //途经点
                null, //示避让区域
                "" //避让道路
        );
        routeSearch.calculateDriveRouteAsyn(query);
    }

    /**
     * 步行模式.
     * @param view view
     */
    public void walkMode(View view) {
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
                startPoint, endPoint);
        RouteSearch.WalkRouteQuery query = new RouteSearch.WalkRouteQuery(
                fromAndTo, RouteSearch.WalkDefault);
        routeSearch.calculateWalkRouteAsyn(query);
    }

    /**
     * 公交模式.
     * @param view view
     */
    public void busMode(View view) {
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
                startPoint, endPoint);
        RouteSearch.BusRouteQuery query = new RouteSearch.BusRouteQuery(
                fromAndTo, //路径规划的起点和终点
                RouteSearch.BusDefault, //公交查询模式
                "010", //公交查询城市区号
                0 //是否计算夜班车，0表示不计算
        );
        routeSearch.calculateBusRouteAsyn(query);
    }

    /**
     * 骑行模式.
     * @param view view
     */
    public void rideMode(View view) {
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
                startPoint, endPoint);
        RouteSearch.RideRouteQuery query = new RouteSearch.RideRouteQuery(
                fromAndTo, RouteSearch.RidingDefault);
        routeSearch.calculateRideRouteAsyn(query);
    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    DrivePath drivePath = result.getPaths().get(0);
                    DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
                            RouteSearchActivity.this, aMap, drivePath,
                            result.getStartPos(),
                            result.getTargetPos(), null);
                    drivingRouteOverlay.setNodeIconVisibility(false);//设置节点marker是否显示
                    drivingRouteOverlay.removeFromMap();
                    drivingRouteOverlay.addToMap();
                    drivingRouteOverlay.zoomToSpan();
                } else {
                    Toast.makeText(RouteSearchActivity.this, "对不起，没有搜索到相关数据",
                            Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(RouteSearchActivity.this, "对不起，没有搜索到相关数据",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(RouteSearchActivity.this, "onDriveRouteSearched error.[" + errorCode + "]",
                    Toast.LENGTH_SHORT).show();
        }

        List<DrivePath> drivePathList = result.getPaths();
        DrivePath drivePath = drivePathList.get(0);
        List<DriveStep> steps = drivePath.getSteps();
        for (DriveStep step : steps) {
            List<LatLonPoint> polyline = step.getPolyline();
            List<TMC> tmcList = step.getTMCs();
            for(TMC tmc : tmcList) {
                String status = tmc.getStatus();
                List<LatLonPoint> polyline1 = tmc.getPolyline();
            }
        }

    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult result, int errorCode) {
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    WalkPath walkPath = result.getPaths()
                            .get(0);
                    WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(
                            RouteSearchActivity.this, aMap, walkPath,
                            result.getStartPos(),
                            result.getTargetPos());
                    walkRouteOverlay.setNodeIconVisibility(true);//设置节点marker是否显示
                    walkRouteOverlay.removeFromMap();
                    walkRouteOverlay.addToMap();
                    walkRouteOverlay.zoomToSpan();
                } else {
                    Toast.makeText(RouteSearchActivity.this, "对不起，没有搜索到相关数据",
                            Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(RouteSearchActivity.this, "对不起，没有搜索到相关数据",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(RouteSearchActivity.this, "onWalkRouteSearched error.[" + errorCode + "]",
                    Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onBusRouteSearched(BusRouteResult result, int errorCode) {
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    List<BusPath> busPath = result.getPaths();
                    BusRouteOverlay busRouteOverlay = new BusRouteOverlay(
                            RouteSearchActivity.this, aMap, busPath.get(0),
                            result.getStartPos(),
                            result.getTargetPos());
                    busRouteOverlay.setNodeIconVisibility(true);//设置节点marker是否显示
                    busRouteOverlay.removeFromMap();
                    busRouteOverlay.addToMap();
                    busRouteOverlay.zoomToSpan();
                } else {
                    Toast.makeText(RouteSearchActivity.this, "对不起，没有搜索到相关数据",
                            Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(RouteSearchActivity.this, "对不起，没有搜索到相关数据",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(RouteSearchActivity.this, "onWalkRouteSearched error.[" + errorCode + "]",
                    Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRideRouteSearched(RideRouteResult result, int errorCode) {
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    RidePath ridePath = result.getPaths()
                            .get(0);
                    RideRouteOverlay rideRouteOverlay = new RideRouteOverlay(
                            this, aMap, ridePath,
                            result.getStartPos(),
                            result.getTargetPos());
                    rideRouteOverlay.setNodeIconVisibility(true);//设置节点marker是否显示
                    rideRouteOverlay.removeFromMap();
                    rideRouteOverlay.addToMap();
                    rideRouteOverlay.zoomToSpan();
                } else {
                    Toast.makeText(RouteSearchActivity.this, "对不起，没有搜索到相关数据",
                            Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(RouteSearchActivity.this, "对不起，没有搜索到相关数据",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(RouteSearchActivity.this, "onWalkRouteSearched error.[" + errorCode + "]",
                    Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}
